package me.pearjelly.service.impl;

import java.util.Date;
import java.util.Set;

import me.pearjelly.dao.PicDao;
import me.pearjelly.dao.PicGroupDao;
import me.pearjelly.dao.SelectorDao;
import me.pearjelly.exception.PicExistException;
import me.pearjelly.exception.ProcessPicFailException;
import me.pearjelly.model.Category;
import me.pearjelly.model.Crawler;
import me.pearjelly.model.Image;
import me.pearjelly.model.PageContext;
import me.pearjelly.model.Pic;
import me.pearjelly.model.PicGroup;
import me.pearjelly.model.Selector;
import me.pearjelly.service.ProcessPicService;
import me.pearjelly.util.JpegMetadataUtil;
import me.pearjelly.util.PageContextUtil;
import me.pearjelly.util.SourceParseUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.drew.metadata.Metadata;

@Service
public class ProcessPicServiceImpl implements ProcessPicService {

	private PicGroup getRelatedPicGroup(PageContext pageContext, Crawler crawler) {
		Set<Selector> selectors = crawler.getSelectors();
		String refer = PageContextUtil.getUrlWithoutPageNo(pageContext,crawler.getGroupPageReg());
		PicGroup picGroup = picGroupDao.findByRefer(refer);
		Set<Category> categories = crawler.getCategories();
		if (null == picGroup) {
			picGroup = new PicGroup();
			picGroup.setName(pageContext.getTitle());
			SourceParseUtil.parsePicGroup(pageContext.getHtml(), selectors,
					picGroup);
			picGroup.setCateId(SourceParseUtil.choosePicGroupId(picGroup, categories, defaultCategoryId));
			picGroup.setCreateTime(new Date());
			picGroup.setTemplateId(defaultTemplateId);
			picGroup.setAuthor(defaultAuthor);
			picGroup.setOperator(defaultOperator);
			picGroup.setEndPageStyle(defaultEndPageStyle);
			picGroup.setPriority(defaultPriority);
			picGroup.setRefer(refer);
			picGroupDao.save(picGroup);
		}
		return picGroup;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Pic addPic(Image image) throws ProcessPicFailException,
			PicExistException {
		PageContext imageContext = image.getPageContext();
		String md5 = PageContextUtil.getMd5(imageContext);
		Pic existPic = picDao.findByMd5(md5);
		if (existPic != null) {
			PageContextUtil.rememberMd5(md5);
			throw new PicExistException();
		}
		Metadata imageMedaData = image.getMetadata();
		Crawler crawler = image.getCrawler();
		PageContext pageContext = PageContextUtil.getParent(imageContext);
		if (null == pageContext) {
			throw new ProcessPicFailException();
		}
		Set<Selector> selectors = crawler.getSelectors();
		PicGroup picGroup = getRelatedPicGroup(pageContext, crawler);
		Pic pic = new Pic();
		pic.setName(pageContext.getTitle());
		SourceParseUtil.parsePic(pageContext.getHtml(), selectors, pic);
		pic.setGroupId(picGroup.getId());
		pic.setExtension(PageContextUtil.getExtension(imageContext));
		pic.setWidth(JpegMetadataUtil.getWith(imageMedaData));
		pic.setHeight(JpegMetadataUtil.getHeight(imageMedaData));
		pic.setMd5(md5);
		pic.setRefer(PageContextUtil.getUrl(pageContext));
		picDao.save(pic);
		return pic;
	}

	private Long defaultCategoryId = 914010L;
	private Integer defaultTemplateId = 3;
	private Integer defaultPriority = 30;
	private String defaultAuthor = "责任编辑：管理员";
	private String defaultOperator = "catcher";
	private String defaultEndPageStyle = "B";
	private PicGroupDao picGroupDao;
	@SuppressWarnings("unused")
	private SelectorDao selectorDao;
	private PicDao picDao;

	@Autowired
	public void setSelectorDao(SelectorDao selectorDao) {
		this.selectorDao = selectorDao;
	}

	@Autowired
	public void setPicGroupDao(PicGroupDao picGroupDao) {
		this.picGroupDao = picGroupDao;
	}

	@Autowired
	public void setPicDao(PicDao picDao) {
		this.picDao = picDao;
	}
}
